package com.xiaof.store.controller;

import com.xiaof.store.entity.Address;
import com.xiaof.store.service.IAddressService;
import com.xiaof.store.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import javax.xml.ws.soap.Addressing;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @ProjectName: store
 * @Package: com.xiaof.store.controller
 * @ClassName: AddressController
 * @Author: xiaof
 * @Description:
 * @Date: 2022/1/6 21:38
 * @Version: 1.0
 */
@RestController
@RequestMapping("addresses")
public class AddressController extends BaseController {
    @Autowired
    private IAddressService addressService;

    @RequestMapping("add_new_address")
    public JsonResult<Void> addNewAddress(Address address, HttpSession session) {
        // 从Session中获取uid和username
        Integer uid = getUidFromSession(session);
        String username = getUsernameFromSession(session);

        // 调用业务对象的方法执行业务
        addressService.addNewAddress(uid, username, address);
        // 响应成功
        return new JsonResult<Void>(OK);
    }
    @RequestMapping({"","/"})
    public JsonResult<List<Address>> getByUid(HttpSession session) {
        List<Address> data = addressService.getByUid(getUidFromSession(session));
        return new JsonResult<>(OK,data);
    }
    @RequestMapping("/{aid}/set_default")
    public JsonResult<Void> setDefault(@PathVariable("aid")Integer aid,HttpSession session){
        Integer uid = getUidFromSession(session);
        String username = getUsernameFromSession(session);
        addressService.setDefault(aid,uid,username);
        return new JsonResult<>(OK);

    }
    @RequestMapping("/{aid}/delete")
    public JsonResult<Void> delete(@PathVariable("aid")Integer aid,HttpSession session){
        addressService.delete(aid,getUidFromSession(session),getUsernameFromSession(session));
        return new JsonResult<>(OK);

    }
}
